/**
 * Copyright © 2018
 * 
 */
package com.adds.lvds.controller.log;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.adds.lvds.dao.impl.log.LogMapper;
import com.adds.lvds.model.log.Log;
import com.adds.lvds.security.acegi.utils.Common;
import com.adds.lvds.security.acegi.utils.CurrentUserUtils;

/**
 * @author: zhangjian
 * @date: 2018年7月28日 上午11:34:16
 * @desc TODO
 */
@Component
public class LogAopController {
	@Autowired
	private LogMapper logMapper;

	public Object logAll(ProceedingJoinPoint point) throws Exception {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		Object result = null;
		String methodName = point.getSignature().getName();
		String className = point.getTarget().getClass().getSimpleName();
		String user = null;
		Long start = 0L;
		Long end = 0L;
		String ip = null;
		try {
			start = System.currentTimeMillis();
			result = point.proceed();
			end = System.currentTimeMillis();
			ip = Common.toIpAddr(request);
			user = CurrentUserUtils.getUsername();
		} catch (Throwable e) {
			e.printStackTrace();
		}

		String moduleName = getModuleName(className);
		String opertype = methodName;
		boolean b = false;
		String[] clazzMethod = { "add", "save","insert", "update", "modify", "delete", "edit" };
		for (String string : clazzMethod) {
			if (methodName.indexOf(string) > -1) {
				b = true;
			}
		}
		if (b) {
			Long time = end - start;
			// 获取id值
			UUID uuid = UUID.randomUUID();
			String str = uuid.toString();
			String id = str.replace("-", "");
			Log log = new Log();
			log.setId(id);
			log.setUsername(user);
			log.setModule(moduleName);
			log.setAction(opertype);
			log.setActionTime(time.toString());
			log.setUserIP(ip);
			logMapper.add(log);
		}

		return result;
	}

	public String getModuleName(String className) {
		String moduleName = "";
		if (className.indexOf("Resources") > -1) {
			moduleName = "菜单管理";
		} else if (className.indexOf("Roles") > -1) {
			moduleName = "角色管理";
		} else if (className.indexOf("User") > -1) {
			moduleName = "用户管理";
		} else if (className.indexOf("Code") > -1) {
			moduleName = "代码设置";
		} else if (className.indexOf("Dict") > -1) {
			moduleName = "数据字典";
		} else if (className.indexOf("Organize") > -1) {
			moduleName = "机构管理";
		} else if (className.indexOf("SysUser") > -1) {
			moduleName = "员工管理";
		} else if (className.indexOf("Scheduler") > -1) {
			moduleName = "计划任务";
		} else if (className.indexOf("Item") > -1) {
			moduleName = "数据项配置";
		} else if (className.indexOf("Link") > -1) {
			moduleName = "链路配置";
		} else if (className.indexOf("Scene") > -1) {
			moduleName = "场景配置";
		} else if (className.indexOf("Task") > -1) {
			moduleName = "监控任务配置";
		} else {
			moduleName = className;
		}
		return moduleName;
	}
}
